package cn.liyicun.school.car2.controller.user;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.jfinal.plugin.activerecord.Db;

import cn.liyicun.school.car2.base.BaseUserController;
import cn.liyicun.school.car2.base.LoginUserName;
import cn.liyicun.school.car2.model.DriverInfo;
import cn.liyicun.school.car2.model.DriverOrderInfo;
import cn.liyicun.school.car2.model.LineInfo;
import cn.liyicun.school.car2.model.PlaceInfo;
import cn.liyicun.school.car2.model.UserOrderInfo;
import cn.liyicun.school.car2.service.CacheModelService;

public class LineController extends BaseUserController {

	public void lineList() {

		String sql = "select * from line_info where status =1";
		List<LineInfo> list = LineInfo.dao.find(sql);
		for (LineInfo lineInfo : list) {
			lineInfo.put("begin_place_data", CacheModelService.dao.placeInfo(lineInfo.getBeginPlace()));
			lineInfo.put("end_place_data", CacheModelService.dao.placeInfo(lineInfo.getEndPlace()));
		}
		setAttr("lines", list);

		LoginUserName info = getLoginAccount();

		String sql1 = "select * from user_order_info where user_id =? order by id desc  limit 200";
		List<UserOrderInfo> list1 = UserOrderInfo.dao.find(sql1, info.getId());
		for (UserOrderInfo lineInfo : list1) {
			LineInfo line = CacheModelService.dao.lineInfo(lineInfo.getLineId());
			lineInfo.put("line_info", line);
			lineInfo.put("begin_place_data", CacheModelService.dao.placeInfo(line.getBeginPlace()));
			lineInfo.put("end_place_data", CacheModelService.dao.placeInfo(line.getEndPlace()));
			if (lineInfo.getStatus() == UserOrderInfo.status_take) {
				DriverOrderInfo or = DriverOrderInfo.dao.findById(lineInfo.getDrOrderId());
				or.put("driver", DriverInfo.dao.findById(or.getDrId()));
				lineInfo.put("driverOrderInfo", or);
				String sql12 = "select order_name,order_phone from user_order_info where dr_order_id=? and id<>?";
				List<UserOrderInfo> peers = UserOrderInfo.dao.find(sql12, lineInfo.getDrOrderId(), lineInfo.getId());
				lineInfo.put("peers", peers);
			}

			// String sql1 = "select i.* from line_place_rela r join place_info
			// i on r.place_id=i.id where r.line_id=?";
			// lineInfo.put("places", Db.find(sql1, lineInfo.getId()));
		}
		setAttr("orders", list1);

	}

	public void callCar() {
		Integer line_id = getParaToInt("line_id", 0);
		// Integer place_id = getParaToInt("place_id", 0);

		verification(line_id <= 0, "line_id不能为空");
		// verification(place_id <= 0, "place_id不能为空");
		String sql = "select * from line_info where status =1 and id=?";
		LineInfo lineInfo = LineInfo.dao.findFirst(sql, line_id);

		lineInfo.put("begin_place_data", CacheModelService.dao.placeInfo(lineInfo.getBeginPlace()));
		lineInfo.put("end_place_data", CacheModelService.dao.placeInfo(lineInfo.getEndPlace()));

		List<LineInfo> list = new ArrayList<>();
		list.add(lineInfo);
		setAttr("line", lineInfo);
		// setAttr("place_id", place_id);
		// setAttr("lines", list);

		LoginUserName info = getLoginAccount();
		setAttr("loginInfo", info);

	}

	// line_id=2&up_place_id=2&order_phone=13800138001&other_req=
	public void createUserOrder() {
		LoginUserName info = getLoginAccount();

		Long count = Db.queryLong("select count(*) from user_order_info where user_id=? and status not in (?,?)",
				info.getId(), UserOrderInfo.status_finish, UserOrderInfo.status_cancel);
		verification(count > 0, "有正在进行的单,不能再创建," + count);
		Integer line_id = getParaToInt("line_id", 0);

		verification(line_id <= 0, "line_id不能为空");
		String order_phone = getPara("order_phone", "");
		String other_req = getPara("other_req", "");
		verification(!order_phone.matches("1\\d{10}"), "手机号码不对!");
		LineInfo lineInfo = LineInfo.dao.findById(line_id);
		verification(lineInfo == null, "线路不存在");
		verification(lineInfo.getStatus() != 1, "线路停运!");

		UserOrderInfo order = new UserOrderInfo();
		order.setCreateTime(new Date());
		order.setLineId(line_id);
		order.setOtherReq(other_req);
		// order.set
		order.setStatus(UserOrderInfo.status_call);

		order.setUserId(info.getId());
		order.setOrderPhone(order_phone);
		order.setOrderName(info.getNickname());
		order.setDrOrderId(0L);
		order.save();
		myRenderJsonSuccess("叫车成功,司机马上联系您!");

	}

	public void cancelOrder() {
		LoginUserName user = getLoginAccount();
		long id = getParaToLong("id");
		UserOrderInfo info = UserOrderInfo.dao.findById(id);

		verification(info == null, "非法访问!");
		verification(info.getUserId() != user.getId().longValue(), "非法访问!");

		UserOrderInfo info1 = new UserOrderInfo();
		info1.setId(id);
		info1.setCancelTime(new Date());
		info1.setStatus(UserOrderInfo.status_cancel);
		info1.update();
		myRenderJsonSuccess("取消成功!");

	}

}
